import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import router from '@/router'
import store from '@/store'
import getPageTitle from '@/utils/get-page-title'
router.beforeEach(async(to, from, next) => {
  NProgress.start()// 开启进度条
  console.log('去哪儿', to)
  console.log('从哪来', from)
  document.title = getPageTitle(to.meta.title)
  const token = store.state.user.token
  const whiteList = ['/login', '/404']
  if (token) {
    console.log('已经登录')
    if (to.path === '/login') {
      console.log('您已经登录,就不能登录了')
      next('/')
      NProgress.done() // 手动关闭进度条
    } else {
      console.log('已经登录,去别的页面,放行')
      if (!store.state.user.userInfo.userId) {
        await store.dispatch('user/updateUserInfo')
      }
      next()
    }
  } else {
    console.log('没有登录')
    if (whiteList.includes(to.path)) {
      console.log('您拥有白名单 放行')
      next()
    } else {
      console.log('您没有白名单,请先登录')
      next('/login')
      NProgress.done() // 手动关闭进度条
    }
  }
})
router.afterEach(() => {
  NProgress.done() // 关闭进度条
})
